/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.controller.exception;

import fr.adama.exception.ArchiveException;
import fr.adama.exception.BulletinFileNotFoundException;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author mini-john
 */
@ControllerAdvice
public class AdamaControllerExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(AdamaControllerExceptionHandler.class);

    @ExceptionHandler(value = BulletinFileNotFoundException.class)
    public ModelAndView bulletinFileNotFoundException(HttpServletRequest req, Exception e) throws Exception {
        // If the exception is annotated with @ResponseStatus rethrow it and let
        // the framework handle it - like the OrderNotFoundException example
        // at the start of this post.
        // AnnotationUtils is a Spring Framework utility class.
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
            throw e;
        }

        BulletinFileNotFoundException ex = (BulletinFileNotFoundException) e;
        logger.error("Une erreur est survenue lors du téléchargement du bulletin {}", ex.getBulletin().getId(), e);
        // Otherwise setup and send the user to a default error-view.
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.addObject("bulletin", ex.getBulletin());
        mav.setViewName("/bulletin/404");
        return mav;
    }

    @ExceptionHandler(value = ArchiveException.class)
    public ModelAndView archiveException(HttpServletRequest req, Exception e) throws Exception {
        // If the exception is annotated with @ResponseStatus rethrow it and let
        // the framework handle it - like the OrderNotFoundException example
        // at the start of this post.
        // AnnotationUtils is a Spring Framework utility class.
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
            throw e;
        }

        ArchiveException ex = (ArchiveException) e;
        logger.error("Une erreur est survenue lors de l'extration de l'archive {}", ex.getFile().getAbsolutePath(), e);
        // Otherwise setup and send the user to a default error-view.
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.addObject("archive", ex.getFile());
        mav.setViewName("/archive/404");
        return mav;
    }

}
